require("./index.scss");
var STip = ZFT.Util.STip;
var Ajax = ZFT.Util.Ajax;
var ParseTemplate = ZFT.Util.ParseTemplate;
var Pagination = require("COMMON/modules/pagination-x/v1.0/");
var Template = {
    resultList: ParseTemplate(require("./tpl/resultList.xtpl")),
    modal: ParseTemplate(require("./tpl/confmodal.xtpl"))
}
var Main = ZFT.Util.Class({
    container: $("body"),
    EVENTS: {
        "click .cancel,.close": "closeModal"
    },
    init: function () {
        var _this = this;
        //初始化分页
        var pagination = this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        })
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.getResultList(toPage);
        });
        _this.getResultList(1);
        _this.initDatepicker();
        _this.bindClickEvent();
    },
    bindClickEvent: function () {
        var _this = this;
        $("#getResultBtn").on("click", function (e) {
            _this.getResultList(1);
        });

    },
    getResultList: function (toPage) {
        if (!toPage) toPage = 1;
        var _this = this;
        var pagination = $(".paginationWrap");
        var pageSize = pagination.attr("data-size");
        var tbody = $("#tbody");
        var param = {
            begin: $("#beginDate").val(),
            type:$("#fileType").val(),
            end: $("#endDate").val(),
            varify_status: $("#isCheck").val(),
            status: $("#checkResult").val(),
            page: toPage,
            page_size: pageSize ? pageSize : 10
        }
        Ajax("/r/File_getFile", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function () {
                pagination.hide();
                var loading = '<tr><td colspan="7">正在加载,请稍后...</td></tr>'
                tbody.html(loading);
            },
            success: function (res) {
                if (res.code == 200) {
                    var data = res.data;
                    var resultList = Template.resultList({ data: data.list });
                    tbody.html(resultList);
                    _this.buildBillOrbuildList();
                    _this.pagination.render({ current: toPage, toPage: toPage, total: data.total })
                } else {
                    tbody.html('<tr><td colspan="7">' + res.msg + '</td></tr>')
                }
            }
        })
    },
    //生成账单或生成审批列表
    buildBillOrbuildList: function () {
        var _this = this;
        $("#tbody .u-billBtn,#tbody .u-listBtn").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var id = tarBtn.attr("data-id");
            var tarType = tarBtn.attr("data-t");
            var title = tarBtn.text();
            var modal = $("#modal");
            var html = Template.modal({ data: title });
            var param = {
                file_id: id,
                type: tarType
            }
            modal.html(html);
            $("#confBtn").on("click", function (e) {
                _this.confBuildBillOrList(param, e)
            })
        })
    },
    //确认生成
    confBuildBillOrList: function (param, e) {
        var tarBtn = $(e.currentTarget);
        var _this = this;
        var modal=$("#modal");
        
        Ajax("/r/PropertyManager_generate", {
            type: "POST",
            dataType: "json",
            params:param,
            loading: function () {
                var loading = '请稍后 <img src="//static.louxiaomei.com/assets/build/image/gloading.gif" class="u-loading_icon" alt="" />';
                tarBtn.attr("disabled", "disabled").html(loading);
                setTimeout(function(){
                    STip("success","已提交生成申请。");
                    modal.html("");
                    _this.getResultList(1);
                },3000);
                
            },
            success: function (res) {
                if (res.code != 200) {
                   // clearTimeout(tip);
                    modal.html("");
                    return STip("fail", res.msg);
                }
            }
        })
    },
    //初始化日历插件
    initDatepicker: function () {
        $("#beginDate").datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
        $('#endDate').datepicker({
            format: "yyyy-mm-dd",
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });
    },
    //关闭模态框
    closeModal: function (e) {
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
    }
});

$(function () {
    new Main();
})